package com.my.note.web.controller;

import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

/**
 * @author lilun <a href="mailto:lil@chsi.com.cn">lilun</a>
 * @version $Id$ 2019年04月10 16:20
 */
public abstract class BaseController {

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    protected Map<String, Object> error(Object msg) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("flag", false);
        map.put("msg", msg);
        return map;
    }

    protected Map<String, Object> errors(BindingResult result) {
        StringBuilder builder = new StringBuilder();
        if (result.hasErrors()) {
            for (ObjectError error : result.getAllErrors()) {
                builder.append(error.getDefaultMessage()).append("；");
            }
        }
        return error(builder.toString());
    }

    protected Map<String, Object> success() {
        Map<String, Object> map = new HashMap<>(2);
        map.put("flag", true);
        map.put("msg", "");
        return map;
    }

    protected Map<String, Object> success(Object msg) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("flag", true);
        map.put("msg", msg);
        return map;
    }

}
